rm(list=setdiff(ls(), "twd")) 
setwd(twd)
library(fixest);library(ggmap);library(raster);library(dplyr);library(wru);library(terra) 
library(tidycensus);library(tidyverse);library(zippeR);library(sf);library(tigris);library(stringr)
library(httr)
library(jsonlite)

options(tigris_use_cache = TRUE)


####################################################
####################################################


# Function to geolocate addresses

geolocate_address <- function(address) {
  # Base URL for Nominatim API
  base_url <- "https://nominatim.openstreetmap.org/search"
  
  # API request
  response <- GET(base_url, query = list(
    q = address,
    format = "json",
    addressdetails = 1,
    limit = 1
  ))
  
  # Check for successful response
  if (status_code(response) == 200) {
    data <- fromJSON(content(response, as = "text", encoding = "UTF-8"))
    
    # Check if any results were returned
    if (length(data) > 0) {
      return(list(
        lat = data$lat,
        lon = data$lon,
        display_name = data$display_name
      ))
    } else {
      return(NULL)
    }
  } else {
    stop("Error: Unable to fetch data from Nominatim API.")
  }
}


### read data

data<-read.csv(file=paste(twd,"data/vreg.csv",sep=""))


#######################################################################
### Change this or I will get charged $$$ ###
#register_google(key = "AIzaSyBdi03sfDqFwaUXWTM_822sgo_m6q6_7gs")

## Subset to Active voters registered by our baseline election 
data<-data[data$cde_registrant_status=="A   ",]
data<-data[as.Date(data$date_of_registration,format= "%m/%d/%Y") < "2018-11-06",]

### Make Treatment Indicators ### 

data$moved<-0
data$consolidated<-0

## Official Moved and Consolidated Locations
 
moved_places<-c("DUPONT TYLER MIDDLE SCHOOL","HERMITAGE PRESBYTERIAN CHURCH","RUBY MAJOR ELEMENTARY SCHOOL" ,
                "HERMITAGE HILLS BAPTIST CHURCH","HERMITAGE COMMUNITY CENTER", "HERMITAGE SCHOOL",
                "TENNESSEE SCHOOL FOR THE BLIND", "TWO RIVERS SCHOOL","DONELSON HEIGHTS UMC",
                "ROSS ELEMENTARY SCHOOL","EAST PARK COMMUNITY CENTER","SHELBY COMMUNITY CENTER",
                "CORA HOWE ELEMENTARY SCHOOL","MARTHA OBRYAN CENTER",
                "15TH AVENUE BAPTIST CHURCH","UNION HALL 737","WATKINS PARK COMMUNITY CENTER",
                "ROBERT CHURCHWELL MUSEUM MAGNET SCHOOL","HAYNES MIDDLE SCHOOL",
                "LOOBY COMMUNITY CENTER", "NEW BRICK CHURCH MIDDLE SCHOOL"
)

consolidated_places<-c("DONELSON PRESBYTERIAN CHURCH","PEARL COHN SCHOOL",
                       "CLEVELAND ST PARK COMM CENTER","WHITES CREEK PK FIRE HALL # 25","NORTHWEST YMCA","BELLSHIRE ELEMENTARY SCHOOL")

data$moved<-ifelse(data$polling_place_text_name %in% moved_places,1,data$moved)
data$consolidated<-ifelse(data$polling_place_text_name %in% moved_places |data$polling_place_text_name %in% consolidated_places ,1,0)

### Get Polling Place Locations ###

data$polling_address<-paste(paste(data$polling_place_text_name,paste(paste(data$polling_place_text_address1, data$polling_place_text_city,sep=",  ")
                                                                     ,data$polling_place_cde_state, sep=", "),sep=", "), data$polling_place_text_zip5,sep=", ")

## Fix discrepencies in address

data$polling_address<-ifelse(data$polling_address=="UNION HILL BAPTIST CH, 1301 UNION HILL RD,  GOODLETTSVILLE, TN, 37072",
                             "UNION HILL BAPTIST CHURCH, 1301 UNION HILL RD,  GOODLETTSVILLE, TN, 37072",data$polling_address)

data$polling_address<-ifelse(data$polling_address=="MADISON PARK COMMUNITY CENTER, DELAWARE AND DUPONT ST,  MADISON, TN, 37115",
                             "MADISON COMMUNITY CENTER, DELAWARE AND DUPONT ST,  MADISON, TN, 37115",data$polling_address)

data$polling_address<-ifelse(data$polling_address=="SCOTTSBORO COMMUNITY CENTER, 5102 OLD HYDES FERRY RD,  NASHVILLE, TN, 37218", 
                             "SCOTTSBORO COMMUNITY CLUB, 5102 OLD HYDES FERRY RD,  NASHVILLE, TN, 37218",data$polling_address)

## Get unique polling places

unique_poll<-data.frame(unique(data$polling_address))

## This Calls GOOGLE API, DON'T RE-RUN COSTS $$$

#lat<-lon<-rep(NA,nrow(unique_poll))

#for(i in 1:nrow(unique_poll)){
#  out_lookup<-geocode(unique_poll[i,])
#  lat[i]<-out_lookup$lat
#  lon[i]<-out_lookup$lon
#  }

#polling_place_lat_lon<-data.frame(unique_poll,lat,lon)

#write.csv(polling_place_lat_lon,paste(twd,"data/made_data/polling_place_lat_lon.csv",sep=""))

## Google API polling place lat long

polling_place_lat_lon<-read.csv(file= paste(twd,"data/made_data/polling_place_lat_lon.csv",sep=""))

############## Get Movers home lat/long ###########################
### Subset to movers ###
movers<-data[data$moved==1,]

## Address for movers
movers$home_address<-paste(paste(paste(paste(paste(movers$text_res_address_nbr, movers$text_street_name, sep=" "), movers$cde_street_type,sep=" "),
                                       movers$text_res_city,sep =", "), movers$cde_res_state, sep=", "), movers$text_res_zip5,sep=", ")
## This Calls GOOGLE API, DON'T RE-RUN COSTS $$$

#unique_home_addres<-unique(movers$home_address)
#lat<-lon<-rep(NA,length(unique_home_addres))
#for(i in 1:length(unique_home_addres)){
#  out_lookup<-geocode(unique_home_addres[i])
#  lat[i]<-out_lookup$lat
#  lon[i]<-out_lookup$lon
#}
#unique_home_address<-data.frame(cbind(unique_home_addres,lat,lon))
#write.csv(unique_home_address,paste(twd,"data/made_data/home_lat_lon.csv",sep=""))

## Google API lat longs for movers 
unique_home_address<-read.csv(paste(twd,"data/made_data/home_lat_lon.csv",sep=""))
unique_home_address<-unique_home_address[,2:4]
names(unique_home_address)<-c("home_address","lat_hom","lon_hom")
movers<-merge(movers,unique_home_address,by="home_address")

## Merge w/ assigned polling place lat/long
assigned_polling_place<-polling_place_lat_lon[,2:4]
names(assigned_polling_place)<-c("polling_address","lat_assigned_poll","lon_assigned_poll")
movers<-merge(movers,assigned_polling_place,by="polling_address")

##############################
### Assigned Realized Address for movers

movers$realized_address<-NA
movers$realized_address<-ifelse(movers$polling_place_text_name %in%
                                  c( "ROSS ELEMENTARY SCHOOL","EAST PARK COMMUNITY CENTER","SHELBY COMMUNITY CENTER",
                                     "CORA HOWE ELEMENTARY SCHOOL","MARTHA OBRYAN CENTER"),
                                "CLEVELAND ST PARK COMM CENTER",movers$realized_address)

movers$realized_address<-ifelse(movers$polling_place_text_name %in%
                                  c("DUPONT TYLER MIDDLE SCHOOL","HERMITAGE PRESBYTERIAN CHURCH","RUBY MAJOR ELEMENTARY SCHOOL" ,
                                    "HERMITAGE HILLS BAPTIST CHURCH","HERMITAGE COMMUNITY CENTER", "HERMITAGE SCHOOL",
                                    "TENNESSEE SCHOOL FOR THE BLIND", "TWO RIVERS SCHOOL","DONELSON HEIGHTS UMC"),
                                "DONELSON PRESBYTERIAN CHURCH" ,movers$realized_address)

movers$realized_address<-ifelse(movers$polling_place_text_name %in%
                                  c("15TH AVENUE BAPTIST CHURCH","UNION HALL 737","WATKINS PARK COMMUNITY CENTER",
                                    "ROBERT CHURCHWELL MUSEUM MAGNET SCHOOL"),
                                "PEARL COHN SCHOOL" ,movers$realized_address)

movers$realized_address<-ifelse(movers$polling_place_text_name  =="HAYNES MIDDLE SCHOOL","WHITES CREEK PK FIRE HALL # 25", 
                                movers$realized_address)

movers$realized_address<-ifelse(movers$polling_place_text_name  =="LOOBY COMMUNITY CENTER","NORTHWEST YMCA", 
                                movers$realized_address)

movers$realized_address<-ifelse(movers$polling_place_text_name  =="NEW BRICK CHURCH MIDDLE SCHOOL","BELLSHIRE ELEMENTARY SCHOOL", 
                                movers$realized_address)


############# Unie home addresses for moves
home_assigned<-movers[duplicated(movers$home_address)==F, c("home_address","polling_address") ]


### Don't Run this costs $$$$$$
#distance<-time<-rep(NA,nrow(home_assigned))
#for(i in 1:nrow(home_assigned)){
#out_dist<-mapdist(home_assigned[i,1],home_assigned[i,2])
#distance[i]<-out_dist$km
#time[i]<-out_dist$minutes
#print(i)
#}
#home_assigned$distance<-distance
#home_assigned$time<-time
#write.csv(home_assigned,"~/Dropbox/Tornado Turnout/TN Voter File - Davidson County Election Commission/made_data/home_assigned_dist.csv")

## Get Google API distances for the assigned 
home_assigned<-read.csv(paste(twd,"data/made_data/home_assigned_dist.csv",sep=""))
home_assigned<-home_assigned[,2:ncol(home_assigned)]

## Mege in the above distances
names(home_assigned)<-c("home_address","polling_address","distance_assigned","time_assigned")
movers<-merge(movers,home_assigned[,c(1,3:4)],by="home_address")
names(movers)[names(movers)== "realized_address"]<-"realized_poll_name"

## get realized polling places
realized_polling_place<-assigned_polling_place
realized_polling_place<-realized_polling_place[do.call(rbind,strsplit(realized_polling_place$polling_address,","))[,1] %in% movers$realized_poll_name,]
realized_polling_place$realized_poll_name<-do.call(rbind,strsplit(realized_polling_place$polling_address,","))[,1]
names(realized_polling_place)<-c("realized_polling_address","lat_realized_poll","lon_realized_poll","realized_poll_name")

## Merge in to movers
movers<-merge(movers,realized_polling_place,by="realized_poll_name")

## Don't run costs $$$$$$
#home_realized<-movers[duplicated(movers$home_address)==F, c("home_address","realized_polling_address") ]
#distance<-time<-rep(NA,nrow(home_realized))
#for(i in 1:nrow(home_realized)){
#out_dist<-mapdist(home_realized[i,1],home_realized[i,2])
#distance[i]<-out_dist$km
#time[i]<-out_dist$minutes
#print(i)
#}
#home_realized$distance<-distance
#heahome_realized$time<-time
#write.csv(home_realized,paste(twd,data/made_data/home_realized_dist.csv",sep=""))

### Google maps distance to realized polling place 
home_realized<-read.csv(file=paste(twd, "data/made_data/home_realized_dist.csv",sep=""))
home_realized<-home_realized[,2:ncol(home_realized)]
names(home_realized)[3:4]<-c("distance_realized","time_realized")
home_realized<-home_realized[,c(1,3:4)]
## merge in
movers<-merge(home_realized,movers,by="home_address")


########## Get Distances to the big polling centers #################
### Do not run, costs $$$$$

#address_gov1<-"1417 Murfreesboro PikeNashville, TN 37217"
#address_gov2<-"800 2nd Ave SNashville, TN 37210"
##distance1<-time1<-distance2<-time2<-rep(NA,nrow(home_assigned))
#for(i in 1:nrow(home_assigned)){
#  out_dist<-mapdist(home_assigned[i,1], address_gov1)
#  distance1[i]<-out_dist$km
#  time1[i]<-out_dist$minutes
#  out_dist2<-mapdist(home_assigned[i,1], address_gov2)
#  distance2[i]<-out_dist2$km
#  time2[i]<-out_dist2$minutes
#  print(i)
#}
#home_assigned$distancegov1<-distance1
#home_assigned$timegov1<-time1
#home_assigned$distancegov2<-distance2
#home_assigned$timegov2<-time2
#write.csv(home_assigned,paste(twd,"data/made_data/home_assigned_distgov.csv",sep=""))

### Get distances to two super sites from google API
home_distgov<-read.csv(paste(twd,"data/made_data/home_assigned_distgov.csv",sep=""))
home_distgov<-home_distgov[,c(3,5,7)]
## Merge in
movers<-merge(movers,home_distgov,by="home_address")


##########################################
######## Now Focus on Non Movers ########
#########################################

non_movers<-data[data$moved==0,]
non_movers$home_address<-paste(paste(paste(paste(paste(non_movers$text_res_address_nbr, non_movers$text_street_name, sep=" "), non_movers$cde_street_type,sep=" "),
                                           non_movers$text_res_city,sep =", "), non_movers$cde_res_state, sep=", "), non_movers$text_res_zip5,sep=", ")

### Don't run Costs $$$$
## Get lat long 
#unique_home_addres_non_movers<-unique(non_movers$home_address)
#lat<-lon<-rep(NA,length(unique_home_addres_non_movers))
#for(i in 1:length(unique_home_addres_non_movers)){
#  try({out_lookup<-geolocate_address(unique_home_addres_non_movers[i])
#  lat[i]<-out_lookup$lat
#  lon[i]<-out_lookup$lon
#  print(i)})
#}
#unique_home_address_non_movers<-data.frame(cbind(unique_home_addres_non_movers,lat,lon))
#unique_home_address_non_movers_complete<-unique_home_address_non_movers[!is.na(unique_home_address_non_movers$lat),]
#write.csv(unique_home_address_non_movers_complete,paste(twd,"data/made_data/home_lat_lon_nomovers_complete.csv",sep=""))

## Get lat long for non movers from google API
unique_home_address_non_movers<-read.csv(paste(twd,"data/made_data/home_lat_lon_nomovers_complete.csv",sep=""))
unique_home_address_non_movers<-unique_home_address_non_movers[,2:4]
names(unique_home_address_non_movers)<-c("home_address","lat","lon")


### Try alternative geolocator for google NAs
#unique_home_addres_non_movers_na<-unique(non_movers[non_movers$home_address %in% unique_home_address_non_movers[,1]==F,"home_address"])
#lat<-lon<-rep(NA,length(unique_home_addres_non_movers_na))
#for(i in 1:length(unique_home_addres_non_movers_na)){
#  try({out_lookup<-geocode(unique_home_addres_non_movers_na[i])
#  lat[i]<-out_lookup$lat
#  lon[i]<-out_lookup$lon
#  print(i)})
#}
#unique_home_address_non_movers_na<-data.frame(cbind(unique_home_addres_non_movers_na,lat,lon))
#write.csv(unique_home_address_non_movers_na,paste(twd,"data/made_data/home_lat_lon_nomovers_na.csv",sep=""))

### Get open source alternative for NAs
unique_home_address_non_movers_na<-read.csv(file=paste(twd,"data/made_data/home_lat_lon_nomovers_na.csv",sep=""))
unique_home_address_non_movers_na<-unique_home_address_non_movers_na[,2:4]
names(unique_home_address_non_movers_na)<-c("home_address","lat","lon")

### all lat long (google and NAs)
home_address_lat_lon_non_movers<-rbind(unique_home_address_non_movers,unique_home_address_non_movers_na)
non_movers<-merge(non_movers,home_address_lat_lon_non_movers,by="home_address")


#########Focus just on consolidated voters ##############

consol<-data[data$consolidated==1,]
consol$home_address<-paste(paste(paste(paste(paste(consol$text_res_address_nbr, consol$text_street_name, sep=" "), consol$cde_street_type,sep=" "),
                                       consol$text_res_city,sep =", "), consol$cde_res_state, sep=", "), consol$text_res_zip5,sep=", ")

consol<-merge(consol,unique_home_address,by="home_address")

assigned_polling_place<-polling_place_lat_lon[,2:4]
names(assigned_polling_place)<-c("polling_address","lat_assigned_poll","lon_assigned_poll")
consol<-merge(consol,assigned_polling_place,by="polling_address")


consol$realized_poll_name<-NA
consol$realized_poll_name<-ifelse(consol$polling_place_text_name %in%
                                    c( "ROSS ELEMENTARY SCHOOL","EAST PARK COMMUNITY CENTER","SHELBY COMMUNITY CENTER",
                                       "CORA HOWE ELEMENTARY SCHOOL","MARTHA OBRYAN CENTER"),
                                  "CLEVELAND ST PARK COMM CENTER",consol$realized_poll_name)

consol$realized_poll_name<-ifelse(consol$polling_place_text_name %in%
                                    c("DUPONT TYLER MIDDLE SCHOOL","HERMITAGE PRESBYTERIAN CHURCH","RUBY MAJOR ELEMENTARY SCHOOL" ,
                                      "HERMITAGE HILLS BAPTIST CHURCH","HERMITAGE COMMUNITY CENTER", "HERMITAGE SCHOOL",
                                      "TENNESSEE SCHOOL FOR THE BLIND", "TWO RIVERS SCHOOL","DONELSON HEIGHTS UMC"),
                                  "DONELSON PRESBYTERIAN CHURCH" ,consol$realized_poll_name)

consol$realized_poll_name<-ifelse(consol$polling_place_text_name %in%
                                    c("15TH AVENUE BAPTIST CHURCH","UNION HALL 737","WATKINS PARK COMMUNITY CENTER",
                                      "ROBERT CHURCHWELL MUSEUM MAGNET SCHOOL"),
                                  "PEARL COHN SCHOOL" ,consol$realized_poll_name)

consol$realized_poll_name<-ifelse(consol$polling_place_text_name  =="HAYNES MIDDLE SCHOOL","WHITES CREEK PK FIRE HALL # 25", 
                                  consol$realized_poll_name)

consol$realized_poll_name<-ifelse(consol$polling_place_text_name  =="LOOBY COMMUNITY CENTER","NORTHWEST YMCA", 
                                  consol$realized_poll_name)

consol$realized_poll_name<-ifelse(consol$polling_place_text_name  =="NEW BRICK CHURCH MIDDLE SCHOOL","BELLSHIRE ELEMENTARY SCHOOL", 
                                  consol$realized_poll_name)

consol$realized_poll_name<-ifelse(is.na(consol$realized_poll_name),consol$polling_place_text_name,consol$realized_poll_name)

realized_totals<-tapply(consol$text_name_first,consol$realized_poll_name,length)
realized_totals<-data.frame(names(realized_totals),realized_totals)
names(realized_totals)<-c("realized_poll_name","total_voters_realized")
consol<-merge(consol,realized_totals,by="realized_poll_name")




##############################################
####### Merge in Everything ##################
##############################################

data$home_address<-paste(paste(paste(paste(paste(data$text_res_address_nbr, data$text_street_name, sep=" "), data$cde_street_type,sep=" "),
                                     data$text_res_city,sep =", "), data$cde_res_state, sep=", "), data$text_res_zip5,sep=", ")
movers_distance<-movers[,c("distance_realized","distance_assigned","distancegov1","distancegov2","lat_hom","lon_hom","text_registrant_id")]
data<-merge(data,movers_distance,by="text_registrant_id",all=T)

### Do lat long for non-movers. This takes too long Should have vectorized. 

for(i in 1:nrow(home_address_lat_lon_non_movers)){
  data$lat_hom<- ifelse(data[,"home_address"]==home_address_lat_lon_non_movers[i,"home_address"], home_address_lat_lon_non_movers[i,"lat"],data$lat_hom)
  data$lon_hom<-ifelse(data[,"home_address"]==home_address_lat_lon_non_movers[i,"home_address"], home_address_lat_lon_non_movers[i,"lon"],data$lon_hom)
  #if(i %in% round(quantile(1:nrow(home_address_lat_lon_non_movers), seq(.1,1,by=.1) ) ) ){print(i/nrow(home_address_lat_lon_non_movers))}
}

movers2<-movers[,c("text_registrant_id","distancegov1","distancegov2")]

### assign zero for non movers
data$distance_assigned<-ifelse(data$moved==0, 0, data$distance_assigned)
data$distance_realized<-ifelse(data$moved==0, 0, data$distance_realized)
data$distancegov1<-ifelse(data$moved==0, 0, data$distancegov1)
data$distancegov2<-ifelse(data$moved==0, 0, data$distancegov2)



### Create total realized turnouts for turnout 


data$realized_poll_name<-NA
data$realized_poll_name<-ifelse(data$polling_place_text_name %in%
                                  c( "ROSS ELEMENTARY SCHOOL","EAST PARK COMMUNITY CENTER","SHELBY COMMUNITY CENTER",
                                     "CORA HOWE ELEMENTARY SCHOOL","MARTHA OBRYAN CENTER"),
                                "CLEVELAND ST PARK COMM CENTER",data$realized_poll_name)
data$realized_poll_name<-ifelse(data$polling_place_text_name %in%
                                  c("DUPONT TYLER MIDDLE SCHOOL","HERMITAGE PRESBYTERIAN CHURCH","RUBY MAJOR ELEMENTARY SCHOOL" ,
                                    "HERMITAGE HILLS BAPTIST CHURCH","HERMITAGE COMMUNITY CENTER", "HERMITAGE SCHOOL",
                                    "TENNESSEE SCHOOL FOR THE BLIND", "TWO RIVERS SCHOOL","DONELSON HEIGHTS UMC"),
                                "DONELSON PRESBYTERIAN CHURCH" ,data$realized_poll_name)
data$realized_poll_name<-ifelse(data$polling_place_text_name %in%
                                  c("15TH AVENUE BAPTIST CHURCH","UNION HALL 737","WATKINS PARK COMMUNITY CENTER",
                                    "ROBERT CHURCHWELL MUSEUM MAGNET SCHOOL"),
                                "PEARL COHN SCHOOL" ,data$realized_poll_name)
data$realized_poll_name<-ifelse(data$polling_place_text_name  =="HAYNES MIDDLE SCHOOL","WHITES CREEK PK FIRE HALL # 25", 
                                data$realized_poll_name)
data$realized_poll_name<-ifelse(data$polling_place_text_name  =="LOOBY COMMUNITY CENTER","NORTHWEST YMCA", 
                                data$realized_poll_name)
data$realized_poll_name<-ifelse(data$polling_place_text_name  =="NEW BRICK CHURCH MIDDLE SCHOOL","BELLSHIRE ELEMENTARY SCHOOL", 
                                data$realized_poll_name)
data$realized_poll_name<-ifelse(is.na(data$realized_poll_name),data$polling_place_text_name,data$realized_poll_name)

#### Total Assigned ###
assigned_N<-tapply(data$text_registrant_id,data$precinct_text_name,length)
assigned_N<-data.frame(names(assigned_N),assigned_N)
names(assigned_N)<-c("precinct_text_name","assigned_N")
data<-merge(data,assigned_N,by="precinct_text_name")
### Total Realized
realized_N<-tapply(data$text_registrant_id,data$realized_poll_name,length)
realized_N<-data.frame(names(realized_N),realized_N)
names(realized_N)<-c("realized_poll_name","realized_N")
data<-merge(data,realized_N,by="realized_poll_name")
data$realized_N<-ifelse(data$consolidated==0,data$assigned_N,data$realized_N)




####Make Damage measures ######

## Damaged Structures
damage<-read.csv(paste(twd,"data/damage_data/Davidson_County_Tornado_Damage_data.csv",sep=""))

### Do not run costs $$$$
### get lat long for power outages

#damages_2<-read.csv(file=paste(twd,"data/damage_data/Power Outages.csv",sep=""))
#damages_2$address<-paste(paste(paste(damages_2$Confirmed.Address, damages_2$City,sep=", "), "TN", sep =", ")
#                         , damages_2$Zip,  sep=", ")
#damages_2$address<-ifelse(as.character(damages_2$Confirmed.Address) == "", 
#                          paste(paste(paste(damages_2$Approximate.Address.or.Region, damages_2$City,sep=", "), "TN", sep =", ")
#                                , damages_2$Zip,  sep=", "),
#                          damages_2$address)
#damages_2<-damages_2[damages_2$Confirmed.Address != "",]                          
#test<-geocode(damages_2$address)
#damages_2$lon<-test$lon
#damages_2$lat<-test$lat
#coordinates(damages_2)<-~lon+lat
##write.csv(damages_2,paste(twd,"data/made_data/power_outages.csv",sep=""))

### Power Outages
damages_2<-read.csv(paste(twd,"data/made_data/power_outages.csv",sep=""))


### Distance measures to power outages and damaged structures

dist_damage_avg<-dist_damage_min<-rep(NA,nrow(data))
dist_power_avg<-dist_power_min<-rep(NA,nrow(data))

for(i in 1:nrow(data)){
  dists<-rep(NA,nrow(damage))
  test<-data.frame(cbind(data$lon_hom[i],
                         data$lat_hom[i],damage$Longitude,damage$Latitude))
  dist_damage_min[i]<-min(pointDistance(test[,1:2],test[,3:4],lonlat=T))
  dist_damage_avg[i]<-mean(pointDistance(test[,1:2],test[,3:4],lonlat=T))
  test2<-data.frame(cbind(data$lon_hom[i],
                          data$lat_hom[i],damages_2$lon,damages_2$lat))
  dist_power_min[i]<-min(pointDistance(test2[,1:2],test2[,3:4],lonlat=T))
  dist_power_avg[i]<-mean(pointDistance(test2[,1:2],test2[,3:4],lonlat=T))
  #if(i %in% round(seq(1,nrow(data),length=11))){print(round(i/nrow(data),3))}
}  


data$dist_damage_min<-dist_damage_min
data$dist_power_min<-dist_power_min
data$dist_damage_avg<-dist_damage_avg
data$dist_power_avg<-dist_power_avg
data<-data[!is.na(data$lon_hom),]

## Turn age  into a date
age_days<-as.Date("03/03/2020",format= "%m/%d/%Y") -as.Date(data$date_of_birth,format= "%m/%d/%Y")
## Number turn it into years
data$age<-as.numeric(age_days/365)

### write output
write.csv(data,paste(twd,"data/made_data/vreg_wgeos_rep.csv",sep=""))




